{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "64506f28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['jl_lport_check(device, ', 'jl_cport_check(device, ', 'jl_utpport_check(device, ', 'jl_extport_check(device, ', 'jl_port_l2c(device, ', 'jl_port_l2p(device, ', 'jl_port_c2l(device, ', 'jl_portmask_l2c(device, ', 'jl_portmask_c2l(device, ', 'JL_CHECK_PORT(device, ', 'JL_CHECK_UTP_PORT(device, ', 'JL_CHECK_EXT_PORT(device, ', 'JL_CHECK_UTP_EXT_PORT(device, ', 'JL_CHECK_CPU_PORT(device, ', 'JL_CHECK_PORTMASK(device, ', 'JL_CHECK_UTP_PORTMASK(device, ', 'JL_CHECK_EXT_PORTMASK(device, ', 'JL_CHECK_CPU_PORTMASK(device, ', 'JL_PORT_L2C_MACRO(device, ', 'JL_PORT_C2L_MACRO(device, ', 'JL_PORT_LMAP_TO_CMAP(device, ', 'JL_PORT_CMAP_TO_LMAP(device, ', 'JL_FOR_EACH_PORT(device, ', 'JL_FOR_EACH_CPORT(device, ', 'JL_FOR_EACH_UTP_PORT(device, ', 'JL_FOR_EACH_EXT_PORT(device, ']\n",
      "['jl_lport_check(pdevice, ', 'jl_cport_check(pdevice, ', 'jl_utpport_check(pdevice, ', 'jl_extport_check(pdevice, ', 'jl_port_l2c(pdevice, ', 'jl_port_l2p(pdevice, ', 'jl_port_c2l(pdevice, ', 'jl_portmask_l2c(pdevice, ', 'jl_portmask_c2l(pdevice, ', 'JL_CHECK_PORT(pdevice, ', 'JL_CHECK_UTP_PORT(pdevice, ', 'JL_CHECK_EXT_PORT(pdevice, ', 'JL_CHECK_UTP_EXT_PORT(pdevice, ', 'JL_CHECK_CPU_PORT(pdevice, ', 'JL_CHECK_PORTMASK(pdevice, ', 'JL_CHECK_UTP_PORTMASK(pdevice, ', 'JL_CHECK_EXT_PORTMASK(pdevice, ', 'JL_CHECK_CPU_PORTMASK(pdevice, ', 'JL_PORT_L2C_MACRO(pdevice, ', 'JL_PORT_C2L_MACRO(pdevice, ', 'JL_PORT_LMAP_TO_CMAP(pdevice, ', 'JL_PORT_CMAP_TO_LMAP(pdevice, ', 'JL_FOR_EACH_PORT(pdevice, ', 'JL_FOR_EACH_CPORT(pdevice, ', 'JL_FOR_EACH_UTP_PORT(pdevice, ', 'JL_FOR_EACH_EXT_PORT(pdevice, ']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\nfor input_file in input_file_list_device:\\n    with open(input_file, \\'r\\') as file:\\n        content = file.read()\\n        \\n    for origin_str, new_str in zip(origin_str_list_device, new_str_list_device):\\n        origin_str_r = origin_str + \\'.*\\'\\n        \\n        def replace(match, ori_str = origin_str, n_str = new_str):\\n            if n_str in match.group():\\n                return match.group()\\n            else:\\n                return re.sub(ori_str, n_str, match.group())\\n        \\n        content = re.sub(origin_str_r, replace, content)\\n\\n        with open(input_file, \\'w\\') as file:\\n            file.write(content)\\n\\n        print(f\"Modified content saved in {input_file}\")\\n'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "import os\n",
    "\n",
    "os.chdir('/home/steveliu/work/jl_switch/sdk/sdk_master/source/libswitch/libjl61xx/src/jl61xx/')\n",
    "\n",
    "gorigin_str_list = ['jl_lport_check\\(', 'jl_cport_check\\(', 'jl_utpport_check\\(', 'jl_extport_check\\(', 'jl_port_l2c\\(', 'jl_port_l2p\\(', 'jl_port_c2l\\(', 'jl_portmask_l2c\\(', 'jl_portmask_c2l\\(', 'JL_CHECK_PORT\\(', 'JL_CHECK_UTP_PORT\\(', 'JL_CHECK_EXT_PORT\\(', 'JL_CHECK_UTP_EXT_PORT\\(', 'JL_CHECK_CPU_PORT\\(', 'JL_CHECK_PORTMASK\\(', 'JL_CHECK_UTP_PORTMASK\\(', 'JL_CHECK_EXT_PORTMASK\\(', 'JL_CHECK_CPU_PORTMASK\\(', 'JL_PORT_L2C_MACRO\\(', 'JL_PORT_C2L_MACRO\\(', 'JL_PORT_LMAP_TO_CMAP\\(', 'JL_PORT_CMAP_TO_LMAP\\(', 'JL_FOR_EACH_PORT\\(', 'JL_FOR_EACH_CPORT\\(', 'JL_FOR_EACH_UTP_PORT\\(', 'JL_FOR_EACH_EXT_PORT\\(']\n",
    "ginput_file_list_device = ['jl61xx_drv_lpd.c', 'jl61xx_drv_port.c', 'jl61xx_drv_stp.c', 'jl61xx_drv_mirror.c', 'jl61xx_drv_pti.c', 'jl61xx_drv_fc.c', 'jl61xx_drv_mib.c', 'jl61xx_drv_cpu.c', 'jl61xx_drv_led.c']\n",
    "ginput_file_list_pdevice = ['jl61xx_drv_trunk.c', 'jl61xx_drv_l2.c', 'jl61xx_drv_vlan.c']\n",
    "ginput_file_list_pDevice = ['jl61xx_drv_storm.c']\n",
    "ginput_file_list_dev = ['jl61xx_drv_qos.c']\n",
    "gnew_str_list_device = []\n",
    "gnew_str_list_pdevice = []\n",
    "gnew_str_list_pDevice = []\n",
    "gnew_str_list_dev = []\n",
    "\n",
    "for item in gorigin_str_list:\n",
    "    gnew_str_list_device.append((item.replace('\\\\', '') + 'device, '))\n",
    "    gnew_str_list_pdevice.append((item.replace('\\\\', '') + 'pdevice, '))\n",
    "    gnew_str_list_pDevice.append((item.replace('\\\\', '') + 'pDevice, '))\n",
    "    gnew_str_list_dev.append((item.replace('\\\\', '') + 'dev, '))\n",
    "                                          \n",
    "\n",
    "\n",
    "def replace_str_in_batch(input_file_list, origin_str_list, new_str_list)\n",
    "    for input_file in input_file_list:\n",
    "        with open(input_file, 'r') as file:\n",
    "            content = file.read()\n",
    "\n",
    "        for origin_str, new_str in zip(origin_str_list, new_str_list):\n",
    "            origin_str_r = origin_str + '.*'\n",
    "\n",
    "            def replace(match, ori_str = origin_str, n_str = new_str):\n",
    "                if n_str in match.group():\n",
    "                    return match.group()\n",
    "                else:\n",
    "                    return re.sub(ori_str, n_str, match.group())\n",
    "\n",
    "            content = re.sub(origin_str_r, replace, content)\n",
    "\n",
    "            with open(input_file, 'w') as file:\n",
    "                file.write(content)\n",
    "\n",
    "            print(f\"Modified content saved in {input_file}\")\n",
    "\n",
    "replace_str_in_batch(ginput_file_list_device, gorigin_str_list, gnew_str_list_device)\n",
    "replace_str_in_batch(ginput_file_list_pdevice, gorigin_str_list, gnew_str_list_pdevice)\n",
    "replace_str_in_batch(ginput_file_list_pDevice, gorigin_str_list, gnew_str_list_pDevice)\n",
    "replace_str_in_batch(ginput_file_list_dev, gorigin_str_list, gnew_str_list_dev)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "82271e16",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Modified content saved in jl61xx_drv_led.c\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "import os\n",
    "\n",
    "os.chdir('/home/steveliu/work/jl_switch/sdk/sdk_master/source/libswitch/libjl61xx/src/jl61xx/')\n",
    "\n",
    "input_file = 'jl61xx_drv_led.c'\n",
    "\n",
    "with open(input_file, 'r') as file:\n",
    "    content = file.read()\n",
    "'''\n",
    "def replace(match):\n",
    "    if 'jl_portmask_l2c(device, ' in match.group():\n",
    "        print('###matched')\n",
    "        print(f\"match group {match.group()}\")\n",
    "        return match.group()\n",
    "    else:\n",
    "        print('####unmatched')\n",
    "        print(f\"match group {match.group()}\")\n",
    "        return re.sub(r'jl_portmask_l2c\\(', 'jl_portmask_l2c(device, ', match.group())\n",
    "'''\n",
    "content = re.sub('jl_portmask_l2c\\(', 'jl_portmask_l2c(device, ', content)\n",
    "\n",
    "with open(input_file, 'w') as file:\n",
    "    file.write(content)\n",
    "\n",
    "print(f\"Modified content saved in {input_file}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75772b24",
   "metadata": {},
   "outputs": [],
   "source": [
    "jl_ret_t jl_lport_check(jl_port_t lport);\n",
    "jl_ret_t jl_cport_check(jl_port_t cport);\n",
    "jl_ret_t jl_utpport_check(jl_port_t lport);\n",
    "jl_ret_t jl_extport_check(jl_port_t lport);\n",
    "jl_port_t jl_port_l2c(jl_port_t lport);\n",
    "jl_port_t jl_port_l2p(jl_port_t lport);\n",
    "jl_port_t jl_port_c2l(jl_port_t cport);\n",
    "jl_ret_t jl_portmask_l2c(jl_portmask_t lportmask, jl_portmask_t *cportmask);\n",
    "jl_ret_t jl_portmask_c2l(jl_portmask_t cportmask, jl_portmask_t *lportmask);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "621a84c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "origin str jl_port_l2p\\(\n",
      "origin str r jl_port_l2p\\(.*\n",
      "new str r jl_port_l2p(device, \n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "Modified content saved in jl61xx_drv_lpd.c\n",
      "origin str jl_port_l2c\\(\n",
      "origin str r jl_port_l2c\\(.*\n",
      "new str r jl_port_l2c(device, \n",
      "###match group jl_port_l2c(port), enable);\n",
      "###match group jl_port_l2c(port), lpdctrl.val[0]);\n",
      "###match group jl_port_l2c(port));\n",
      "###match group jl_port_l2c(port), lpdctrl.val[0]);\n",
      "###match group jl_port_l2c(port));\n",
      "###match group jl_port_l2c(port), lpdsts.val[0]);\n",
      "Modified content saved in jl61xx_drv_lpd.c\n",
      "origin str jl_port_l2p\\(\n",
      "origin str r jl_port_l2p\\(.*\n",
      "new str r jl_port_l2p(device, \n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port)) {\n",
      "match group jl_port_l2p(device, port)) {\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port)) {\n",
      "match group jl_port_l2p(device, port)) {\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "###match group jl_port_l2p(device, port));\n",
      "match group jl_port_l2p(device, port));\n",
      "Modified content saved in jl61xx_drv_port.c\n",
      "origin str jl_port_l2c\\(\n",
      "origin str r jl_port_l2c\\(.*\n",
      "new str r jl_port_l2c(device, \n",
      "###match group jl_port_l2c(port));\n",
      "###match group jl_port_l2c(port));\n",
      "###match group jl_port_l2c(port));\n",
      "###match group jl_port_l2c(port));\n",
      "###match group jl_port_l2c(port));\n",
      "###match group jl_port_l2c(port));\n",
      "###match group jl_port_l2c(port));\n",
      "Modified content saved in jl61xx_drv_port.c\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "import os\n",
    "\n",
    "os.chdir('/home/steveliu/work/jl_switch/sdk/sdk_master/source/libswitch/libjl61xx/src/jl61xx/')\n",
    "\n",
    "input_file_list_device = ['jl61xx_drv_lpd.c', 'jl61xx_drv_port.c']\n",
    "origin_str_list_device = ['jl_port_l2p\\(', 'jl_port_l2c\\(']\n",
    "new_str_list_device = ['jl_port_l2p(device, ', 'jl_port_l2c(device, ']\n",
    "\n",
    "for input_file in input_file_list_device:\n",
    "    with open(input_file, 'r') as file:\n",
    "        content = file.read()\n",
    "        \n",
    "    for origin_str, new_str in zip(origin_str_list_device, new_str_list_device):\n",
    "        origin_str_r = origin_str + '.*'\n",
    "        print(f\"origin str {origin_str}\")\n",
    "        print(f\"origin str r {origin_str_r}\")\n",
    "        print(f\"new str r {new_str}\")\n",
    "        \n",
    "        def replace(match, ori_str = origin_str, n_str = new_str):\n",
    "            print(f\"###match group {match.group()}\")\n",
    "            if n_str in match.group():\n",
    "                print(f\"match group {match.group()}\")\n",
    "                return match.group()\n",
    "            else:\n",
    "                return re.sub(ori_str, n_str, match.group())\n",
    "        \n",
    "        content = re.sub(origin_str_r, replace, content)\n",
    "\n",
    "        with open(input_file, 'w') as file:\n",
    "            file.write(content)\n",
    "\n",
    "        print(f\"Modified content saved in {input_file}\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4e3273f",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_str_list_device = ['jl_lport_check(device, ', 'jl_cport_check(device, ', 'jl_utpport_check(device, ', 'jl_extport_check(device, ', 'jl_port_l2c(device, ', 'jl_port_l2p(device, ', 'jl_port_c2l(device, ', 'jl_portmask_l2c(device, ', 'jl_portmask_c2l(device, ', 'JL_CHECK_PORT\\(', 'JL_CHECK_UTP_PORT\\(', 'JL_CHECK_EXT_PORT\\(', 'JL_CHECK_UTP_EXT_PORT\\(', 'JL_CHECK_CPU_PORT\\(', 'JL_CHECK_PORTMASK\\(', 'JL_CHECK_UTP_PORTMASK\\(', 'JL_CHECK_EXT_PORTMASK\\(', 'JL_CHECK_CPU_PORTMASK\\(', 'JL_PORT_L2C_MACRO\\(', 'JL_PORT_C2L_MACRO\\(', 'JL_PORT_LMAP_TO_CMAP\\(', 'JL_PORT_CMAP_TO_LMAP\\(', 'JL_FOR_EACH_PORT\\(', 'JL_FOR_EACH_CPORT\\(', 'JL_FOR_EACH_UTP_PORT\\(', 'JL_FOR_EACH_EXT_PORT\\(']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2e5e7735",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is a string with a  backslash character\n"
     ]
    }
   ],
   "source": [
    "input_string = \"This is a string with a \\\\ backslash character\"\n",
    "output_string = input_string.replace('\\\\', '')\n",
    "\n",
    "print(output_string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a17b0a86",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
